package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.constant.ConstEnum;
import cc.yugu.yhdw.common.domain.basic.User;
import cc.yugu.yhdw.common.domain.hdw.Supplier;
import cc.yugu.yhdw.common.domain.hdw.SupplierInfo;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.common.entity.tree.Node;
import cc.yugu.yhdw.common.entity.tree.NodeModel;
import cc.yugu.yhdw.common.utils.YhdwUtils;
import cc.yugu.yhdw.webserver.persistence.basic.UserMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.SupplierMapper;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
/**
 * 厂商
 * 
 * @Date:Created in 2018/7/10 17:11
 * @Author:
 * @Modified By: zjn 2018/7/10 17:11
 */ 
@Service
public class SupplierService {

    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    UserMapper userMapper;

    public Supplier find(int id) {
        return supplierMapper.find(id);
    }
    /**
    * 厂商信息输出
    * 
    * @Param: [stream]
    * @Return: void
    * @Author By: zjn 2018/7/10  17:11
    */ 
    public void tree(OutputStream stream) throws IOException {
        // 根据是否启用表示查询
        List<Supplier> supplierList = supplierMapper.findAll(ConstEnum.Flag.TRUE.getValue());
        List<Node<NodeModel>> roots = new ArrayList<Node<NodeModel>>();
        for (Supplier supplier : supplierList) {
            NodeModel nodeModel = new NodeModel();
            Node<NodeModel> root = new Node<NodeModel>(nodeModel);
            nodeModel.setId(supplier.getId());
            nodeModel.setName(supplier.getSupplierName());
            roots.add(root);
        }

        ObjectMapper objectMapper = new ObjectMapper();
        JsonGenerator json = objectMapper.getJsonFactory().createJsonGenerator(stream, JsonEncoding.UTF8);
        json.writeStartArray();
        YhdwUtils.writeJson(roots, json);
        json.writeEndArray();
        json.flush();
        json.close();
    }
    /**
    * 根据厂商类型查询
    * 
    * @Param: [type]
    * @Return: java.util.List<cc.yugu.yhdw.common.domain.hdw.Supplier>
    * @Author By: zjn 2018/7/10  17:19
    */ 
    public List<Supplier> findByType(int type) {
        return supplierMapper.findByType(type);
    }
    /**
    * 分页查询
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/10  17:20
    */ 
    public Page findPage(Supplier search) {
        Page page = search.buildPage();
        page.setTotalItems(supplierMapper.findPageCount(search));
        search.setBeginIndex(page.getOffset());
        page.setResult(supplierMapper.findPageResult(search));
        return page;
    }
    /**
    * 插入
    * 
    * @Param: [supplier]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/10  17:20
    */ 
    public ExtResult insert(Supplier supplier) {
        try {
            supplier.setBalance(0);
            supplier.setCreateTime(new Date());
            supplierMapper.insert(supplier);
        } catch (Exception e) {
            return ExtResult.failResult("操作失败");
        }
        return ExtResult.successResult();
    }
    /**
    * 更新
    * 
    * @Param: [entity]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/10  17:20
    */ 
    public ExtResult update(Supplier entity) {
        try {
            supplierMapper.update(entity);
        } catch (Exception e) {
            return ExtResult.failResult("操作失败");
        }
        return ExtResult.successResult();
    }
    /**
    * 删除
    * 
    * @Param: [id, activeFlag]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/10  17:20
    */ 
    public ExtResult delete(int id, int activeFlag) {
        try {
            userMapper.disable(null, null, id,activeFlag);
            supplierMapper.disable(id,activeFlag);
        } catch (Exception e) {
            return ExtResult.failResult("操作失败");
        }
        return ExtResult.successResult();
    }
}
